#include "firstpage.h"
#include "ui_firstpage.h"
#include "mainwindow.h"
#include "QPainter"
#include "QPaintEvent"
#include "QTimer"
#include "QSound"
#include "gamerulepage.h"
#include "mypushbutton.h"

FirstPage::FirstPage(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::FirstPage)
{
    ui->setupUi(this);
    //设置窗口标题
    this->setWindowTitle("六子棋小游戏");
    this->setFixedSize(2*MARGIN+(BOARD_GARD_SIZE-1)*BLOCK_SIZE+400,2*MARGIN+(BOARD_GARD_SIZE-1)*BLOCK_SIZE);
    this->setWindowIcon(QPixmap(":/res/logo2.jpg")); //设置应用图标

    //创建菜单栏
    QMenuBar * bar = menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu * startMenu = bar->addMenu("开始");

    //创建退出的菜单项
    QAction * quitAction = startMenu->addAction("退出");

    //点击退出，实现退出游戏
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //创建主界面标题
    ui->label->setGeometry(10,10,1250,200);

//////////////////////尝试用图片代替手动创建按钮，效果更好////////////////////////
    //开始按钮
    MyPushButton * startBtn1 = new MyPushButton(":/res/startBtncorrect.png");

    startBtn1->setParent(this);
    startBtn1->move(this->width() * 0.5 -startBtn1->width() * 0.5 ,500);

    gameMainWindow = new MainWindow;

    //监听游戏界面的返回按钮信号
    connect(gameMainWindow,&MainWindow::MainWindowBack,this,[=](){
       this->setGeometry(gameMainWindow->geometry());
       gameMainWindow->hide(); //将选择关卡场景 隐藏掉
       this->show(); //重新显示主场景
    });



    connect(startBtn1,&MyPushButton::clicked,[=](){
        //播放开始音效
         QSound::play(":/res/button.wav");

        //做弹起特效
        startBtn1->zoom1();
        startBtn1->zoom2();

        //延时进入到选择关卡场景
        QTimer::singleShot(500,this,[=](){
            //设置mainwindow场景的位置
            gameMainWindow->setGeometry(this->geometry());

            //自身隐藏
            this->hide();
            //显示选择关卡场景
            gameMainWindow->show();

        });

    });

    //查看规则按钮
    MyPushButton * rulecheckBtn1 = new MyPushButton(":/res/final1.png");

    rulecheckBtn1->setParent(this);
    rulecheckBtn1->move(this->width() * 0.5 -rulecheckBtn1->width() * 0.5 ,650);

    gameRuleCheck = new GameRulePage;

    //监听游戏界面的返回按钮信号
    connect(gameRuleCheck,&GameRulePage::GameRulePageBack,this,[=](){
       this->setGeometry(gameRuleCheck->geometry());
       gameRuleCheck->hide(); //将选择关卡场景 隐藏掉
       this->show(); //重新显示主场景
    });

    connect(rulecheckBtn1,&MyPushButton::clicked,[=](){
        //播放开始音效
        QSound::play(":/res/button.wav");

        //做弹起特效
        rulecheckBtn1->zoom1();
        rulecheckBtn1->zoom2();

        //延时进入到选择关卡场景
        QTimer::singleShot(500,this,[=](){
            //设置mainwindow场景的位置
            gameRuleCheck->setGeometry(this->geometry());

            //自身隐藏
            this->hide();
            //显示选择关卡场景
            gameRuleCheck->show();

        });

    });


}


void FirstPage::paintEvent(QPaintEvent *){
    //设置背景图片
    //创建画家
    QPainter painter(this);
    painter.drawPixmap( 0 , 0 , this->width() , this->height() , QPixmap(":/res/Game_Background.jpg"));
    painter.setRenderHint( QPainter::Antialiasing , true); //抗锯齿,防止图像走样

    //设置主界面小图片（棋盘标志）
    painter.drawPixmap( 460 , 200 , 300 , 300 , QPixmap(":/res/firstPageImage.webp"));
    painter.setRenderHint( QPainter::Antialiasing , true); //抗锯齿,防止图像走样
}

FirstPage::~FirstPage()
{
    delete ui;
}
